package com.goodfox.web.generic.provide.util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 对象转换工具类
 * @author qin gt
 *
 */
public class TransformUtil {

	/**
	 * 十六进制String转Object对象
	 * @param hex
	 * @return
	 */
	public static Object hexString2Object(String hex){
		return byte2Object(hexString2Bytes(hex));
	}
	
	/**
	 * byte[]转Object对象
	 * @param bytes
	 * @return
	 */
	public static Object byte2Object(byte[] bytes){
		Object obj=null;
		try {
			ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
			ObjectInputStream oi = new ObjectInputStream(bi);
			obj = oi.readObject();
			bi.close();
			oi.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return obj;
	}
	
	/**
	 * Object对象转十六进制String
	 * @param obj
	 * @return
	 */
	public static String object2HexString(Object obj){
		return byte2HexString(object2Bytes(obj));
	}
	
	/**
	 * Object对象转byte[]
	 * @param obj
	 * @return
	 */
	public static byte[] object2Bytes(Object obj){
		byte[] bytes= new byte[1024];
		try {
			ByteArrayOutputStream bo = new ByteArrayOutputStream();
			ObjectOutputStream oo = new ObjectOutputStream(bo);
			oo.writeObject(obj);
			bytes = bo.toByteArray();
			bo.close();
			oo.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return bytes;
	}

	/**
	 * byte[]转十六进制String
	 * @param bytes
	 * @return
	 */
	public static String byte2HexString(byte[] bytes){ 
        String hs="";   
        String stmp="";   
        for (int n=0;n<bytes.length;n++) {   
            stmp=(Integer.toHexString(bytes[n] & 0XFF));   
            if (stmp.length()==1){   
                hs=hs+"0"+stmp;   
            }else{   
                hs=hs+stmp;   
            }   
        }   
        return hs.toUpperCase();   
    }  

	/**
	 * 十六进制String转byte[]
	 * @param hex
	 * @return
	 */
	public static byte[] hexString2Bytes(String hex) {
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	/**
	 * char转byte
	 * @param c
	 * @return
	 */
	private static byte toByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(c);
		return b;
	}
	
	/**
	 * 文件转十六进制String
	 * @param f 
	 * @return
	 */
	public static String file2HexString(File f){
		return byte2HexString(file2Bytes(f));
	}
	
	/**
	 * 文件File转byte[]
	 * @param f 
	 * @return
	 */
	public static byte[] file2Bytes(File f) {
		if (f == null) {
			return null;
		}
		try {
			FileInputStream stream = new FileInputStream(f);
			ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = stream.read(b)) != -1) {
				out.write(b, 0, n);
			}
			stream.close();
			out.close();
			return out.toByteArray();
		} catch (IOException ioex) {
			ioex.printStackTrace();
		}
		return null;
	}

	/**
	 * 十六进制String转文件对象File
	 * @param hex 
	 * @param outputFile 文件保存路径
	 * @return
	 */
	public static boolean hexString2File(String hex,String outputFile){
		return bytes2File(hexString2Bytes(hex),outputFile);
	}
	
	/**
	 * byte[]转文件对象File
	 * @param b 
	 * @param outputFile 文件保存路径
	 * @return
	 */
	public static boolean bytes2File(byte[] b, String outputFile) {
		boolean bool=false;
		BufferedOutputStream stream = null;
		File file = null;
		try {
			file = new File(outputFile);
			FileOutputStream fstream = new FileOutputStream(file);
			stream = new BufferedOutputStream(fstream);
			stream.write(b);
			bool=true;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (IOException ioex) {
					ioex.printStackTrace();
				}
			}
		}
		return bool;
	}
	
	/**
	 * byte[]转文件对象File
	 * @param File 
	 * @param outputFile 文件保存路径
	 * @return
	 */
	public static File getBytesFile(byte[] b, String outputFile) {
		BufferedOutputStream stream = null;
		File file = null;
		try {
			file = new File(outputFile);
			FileOutputStream fstream = new FileOutputStream(file);
			stream = new BufferedOutputStream(fstream);
			stream.write(b);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (IOException ioex) {
					ioex.printStackTrace();
				}
			}
		}
		return file;
	}
	
	/**
	 * 输入流转byte[]
	 * @param ins 输入流
	 * @return
	 */
	public static byte[] stream2Bytes(InputStream ins){
		if (ins == null) {
			return null;
		}
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = ins.read(b)) != -1) {
				out.write(b, 0, n);
			}
			ins.close();
			out.close();
			return out.toByteArray();
		} catch (IOException ioex) {
			ioex.printStackTrace();
		}
		return null;
	}

	/**
	 * Html网页文本转普通文本
	 * @param inputString
	 * @return
	 */
	public static String html2Text(String inputString) {
		String htmlStr = inputString; //含html标签的字符串 
		String textStr = "";
		Pattern p_script;
		Matcher m_script;
		Pattern p_style;
		Matcher m_style;
		Pattern p_html;
		Matcher m_html;
		Pattern p_houhtml;
		Matcher m_houhtml;
		Pattern p_spe;
		Matcher m_spe;
		Pattern p_blank;
		Matcher m_blank;
		Pattern p_table;
		Matcher m_table;
		Pattern p_enter;
		Matcher m_enter;

		try {
			String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
			//定义script的正则表达式.
			String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
			//定义style的正则表达式. 
			String regEx_html = "<[^>]+>";
			//定义HTML标签的正则表达式 
			String regEx_houhtml = "/[^>]+>";
			//定义HTML标签的正则表达式 
			String regEx_spe = "\\&[^;]+;";
			//定义特殊符号的正则表达式
			String regEx_blank = " +";
			//定义多个空格的正则表达式
			String regEx_table = "\t+";
			//定义多个制表符的正则表达式
			String regEx_enter = "\n+";
			//定义多个回车的正则表达式

			p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
			m_script = p_script.matcher(htmlStr);
			htmlStr = m_script.replaceAll(""); //过滤script标签

			p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
			m_style = p_style.matcher(htmlStr);
			htmlStr = m_style.replaceAll(""); //过滤style标签 

			p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(htmlStr);
			htmlStr = m_html.replaceAll(""); //过滤html标签 

			p_houhtml = Pattern
					.compile(regEx_houhtml, Pattern.CASE_INSENSITIVE);
			m_houhtml = p_houhtml.matcher(htmlStr);
			htmlStr = m_houhtml.replaceAll(""); //过滤html标签 

			p_spe = Pattern.compile(regEx_spe, Pattern.CASE_INSENSITIVE);
			m_spe = p_spe.matcher(htmlStr);
			htmlStr = m_spe.replaceAll(""); //过滤特殊符号 

			p_blank = Pattern.compile(regEx_blank, Pattern.CASE_INSENSITIVE);
			m_blank = p_blank.matcher(htmlStr);
			htmlStr = m_blank.replaceAll(" "); //过滤过多的空格

			p_table = Pattern.compile(regEx_table, Pattern.CASE_INSENSITIVE);
			m_table = p_table.matcher(htmlStr);
			htmlStr = m_table.replaceAll(" "); //过滤过多的制表符

			p_enter = Pattern.compile(regEx_enter, Pattern.CASE_INSENSITIVE);
			m_enter = p_enter.matcher(htmlStr);
			htmlStr = m_enter.replaceAll(" "); //过滤过多的制表符

			textStr = htmlStr;

		} catch (Exception e) {
			System.err.println("Html2Text: " + e.getMessage());
		}
		//返回文本字符串 
		return textStr;
	}
}
